from os import system

import requests
import json

config = open("./config","r")
display = config.read()
config.close()

def player():
    """ The function takes json information from the site retrowave.ru using the requests method. 
        Then, using the json method, it converts the information to a format to json.
        Then gets the path to the track on the server, turns it into a url and runs it in the mpv player.

        The function does not accept any parameters for input and does not return anything.

    """

    urlOnlineRadioJsonData = 'https://retrowave.ru/api/v1/tracks?cursor=1&limit=1'

    response = requests.get(urlOnlineRadioJsonData)

    jsonTrackData = json.loads(response.text)

    trackPathOnServer = jsonTrackData['body']['tracks'][0]['streamUrl']

    trackUrl = 'https://retrowave.ru' + trackPathOnServer

    if "#" in display or "True" in display:
        mpvPlayTrackUrl = 'mpv ' + trackUrl
        
    elif "False" in display:
        mpvPlayTrackUrl = 'mpv --no-audio-display ' + trackUrl
    else:
        print("Config Error")
        mpvPlayTrackUrl = 'mpv ' + trackUrl

    system(mpvPlayTrackUrl)


while True:
    player()
